package com.apes.scm.masterdata.product.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Objects;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CategoryTag.CONST_TABLE_NAME)
public class CategoryTag extends BaseModel {

    public static final String CONST_TABLE_NAME = "mas_category_tag";
    
    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "商品类目标签")
    @Comment("唯一码")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "category_id", referencedColumnName = "id", nullable = false)
    @Comment("商品类目")
    private Category category;

    @Column(name = "name", nullable = false, length = 120)
    @Comment("标签")
    private String name;

    @Transient
    private String categoryId;

    public String getCategoryId() {
        if (Objects.nonNull(this.category)){
            return category.getId();
        }
        return this.categoryId;
    }

    @Transient
    private String categoryName;

    public String getCategoryName() {
        if (Objects.nonNull(this.category)){
            return category.getName();
        }
        return this.categoryName;
    }

}
